﻿// --- Copyright (c) 2010 Steven Hartgers ---
// Copyright (C) 2010 Steven Hartgers All rights are reserved.
// Author contact: fiinix00/at\gmail.com
// ------------------------------------------------------------------------
// This program is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// This program is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more detail
//  
// You should have received a copy of the GNU General Public License 
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WINSATLib;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace Hack.Windows7
{
    public class WindowsExperience
    {
        [Description("Middle handler for WINSATLib and c# for a structured Intellisence")]
        public class WindowsScore
        {
            public string RatingStateDesc { get; internal set; }
            public object AssessmentDateTime { get; internal set; }
            public ASSESSMENT_STATE AssessmentState { get; internal set; }
            public float SystemRating { get; internal set; }

            public AssessmentInfo MemoryRating { get; internal set; }
            public AssessmentInfo CPURating { get; internal set; }
            public AssessmentInfo HDDRating { get; internal set; }
            public AssessmentInfo Directx3DRating { get; internal set; }
            public AssessmentInfo GFXRating { get; internal set; }

            public Image ScoreImage { get; internal set; }
        }
        /*
        WINSAT_ASSESSMENT_MEMORY = 0,
        WINSAT_ASSESSMENT_CPU = 1,
        WINSAT_ASSESSMENT_DISK = 2,
        WINSAT_ASSESSMENT_D3D = 3,
        WINSAT_ASSESSMENT_GRAPHICS = 4,
        */
        public enum ASSESSMENT_STATE
        {
            WINSAT_ASSESSMENT_STATE_UNKNOWN = 0,
            WINSAT_ASSESSMENT_STATE_MIN = 0,
            WINSAT_ASSESSMENT_STATE_VALID = 1,
            WINSAT_ASSESSMENT_STATE_INCOHERENT_WITH_HARDWARE = 2,
            WINSAT_ASSESSMENT_STATE_NOT_AVAILABLE = 3,
            WINSAT_ASSESSMENT_STATE_MAX = 4,
            WINSAT_ASSESSMENT_STATE_INVALID = 4
        }
        public interface AssessmentInfo
        {
            [DispId(4)]
            string Description { get; }
            [DispId(1)]
            float Score { get; }
            [DispId(3)]
            string Title { get; }
        }
        [STAThread]
        public static WindowsScore GetScore()
        {
            CQueryWinSATClass q = new CQueryWinSATClass();
            if (q.Info.AssessmentState == WINSAT_ASSESSMENT_STATE.WINSAT_ASSESSMENT_STATE_VALID
                      || q.Info.AssessmentState == WINSAT_ASSESSMENT_STATE.WINSAT_ASSESSMENT_STATE_INCOHERENT_WITH_HARDWARE)
            {
                IntPtr t;
                unsafe
                {
                    CProvideWinSATVisualsClass v = new CProvideWinSATVisualsClass();
                    v.get_Bitmap(WINSAT_BITMAP_SIZE.WINSAT_BITMAP_SIZE_NORMAL, q.Info.AssessmentState, q.Info.SystemRating, new IntPtr(&t));
                    return new WindowsScore
                    {
                        /*
                         * get_Bitmap has the following unmanaged signature:
                         *   HRESULT get_Bitmap(
                         *     WINSAT_BITMAP_SIZE bitmapSize,
                         *     WINSAT_ASSESSMENT_STATE state,
                         *     float rating,
                         *     HBITMAP* pBitmap
                         *   );
                         * where HBITMAP is defined as (WinDef.h):
                         *   typedef HANDLE HBITMAP;
                         * 
                         * The last parameter gets translated into an IntPtr which is the pointer (&t) to the pointer (t) to the bitmap.
                         */
                        AssessmentDateTime = q.Info.AssessmentDateTime,
                        AssessmentState = (ASSESSMENT_STATE)q.Info.AssessmentState,
                        RatingStateDesc = q.Info.RatingStateDesc,
                        SystemRating = q.Info.SystemRating,
                        ScoreImage = t != IntPtr.Zero ? Bitmap.FromHbitmap(t) : null
                    };
                }

                //
                // Some formatting stuff.
                //
                //string format = "{0,-18} {1,-56} {2:N1}";
                //string seperator = String.Format(format, new string('-', 18), new string('-', 56), new string('-', 3));

                ////
                //// General rating information.
                ////
                //Console.WriteLine("{0} on {1}", q.Info.RatingStateDesc, q.Info.AssessmentDateTime);
                //Console.WriteLine();

                ////
                //// Get ratings for individual assessment types.
                ////
                //Console.WriteLine(format, "Category", "Description", "Rat");
                //Console.WriteLine(seperator);
                //IEnumerator e = Enum.GetValues(typeof(WINSAT_ASSESSMENT_TYPE)).GetEnumerator();
                //while (e.MoveNext())
                //{
                //    IProvideWinSATAssessmentInfo i = q.Info.GetAssessmentInfo((WINSAT_ASSESSMENT_TYPE)e.Current);
                //    Console.WriteLine(format, i.Title, i.Description, i.Score);
                //}

                ////
                //// Overall system base score.
                ////
                //Console.WriteLine(seperator);
                //Console.WriteLine(format, "Base score:", "Determined by lowest subscore", q.Info.SystemRating);
            }
            // IntPtr t;

            //unsafe
            //{

            //    CProvideWinSATVisualsClass v = new CProvideWinSATVisualsClass();
            //    v.get_Bitmap(WINSAT_BITMAP_SIZE.WINSAT_BITMAP_SIZE_NORMAL, q.Info.AssessmentState, q.Info.SystemRating, new IntPtr(&t));
            //}
            //if (t != IntPtr.Zero)
            //{
            //    //return Bitmap.FromHbitmap(t);
            //}
            
            throw new Exception("Internal error when getting the Windows Experiance Info");
        }
    }
}